package online.xuping.thousandsOfWorlds.common;

import cn.hutool.core.util.NumberUtil;
import online.xuping.thousandsOfWorlds.common.constants.CommonErrorCodeEnum;
import online.xuping.thousandsOfWorlds.common.constants.CommonVOEnum;

import javax.servlet.http.HttpServletRequest;

/**
 * 类的说明
 *
 * @module
 * @Author xuping2
 * @Date 2022/8/4 13:33
 */
public class BaseController {


    /**
     * 通过头信息获取userid
     */
    protected Integer getUserId(HttpServletRequest request) {
        String userId = request.getHeader(CommonVOEnum.USER_ID.getCode());
        if (NumberUtil.isInteger(userId)) {
            return NumberUtil.parseInt(userId);
        }
        throw CommonErrorCodeEnum.USERID_NO_FOUND.exception();
    }

    /**
     * 通过头信息获取userid(string)
     */
    protected String getUserIdStr(HttpServletRequest request) {
        return getUserId(request).toString();
    }
}
